#
#  This is a sample CMakeLists.txt suitable for direct use with a new PETSc application or
#  you can add fragments of material below to an existing applications CMakeLists.txt
#
#  Usage:  To build ex1 from ex1.c
#    rm -fr build
#    mkdir build
#    cd build
#    cmake ..
#    make
#    ./ex1
#
#  By default it gets both the compiler information and the library information from PETSc
#
cmake_minimum_required(VERSION 3.9.0)

set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
set(CMAKE_BUILD_TYPE RELEASE)
if(CMAKE_BUILD_TYPE AND (CMAKE_BUILD_TYPE STREQUAL DEBUG))
   message("Debug mode: ${CMAKE_Fortran_FLAGS_DEBUG}")
elseif(CMAKE_BUILD_TYPE AND (CMAKE_BUILD_TYPE STREQUAL RELEASE))
	#   include(CheckIPOSupported)
	#check_ipo_supported(RESULT _IsIPOSupported)
	#   if(_IsIPOSupported)
	#message(STATUS "Turn on INTERPROCEDURAL_OPTIMIZATION")
	#set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
	#endif()
   message("Release mode: ${CMAKE_Fortran_FLAGS_RELEASE}")
endif()

# set root of location to find PETSc's pkg-config
set(PETSC $ENV{PETSC_DIR}/$ENV{PETSC_ARCH})
set(ENV{PKG_CONFIG_PATH} ${PETSC}/lib/pkgconfig:$ENV{SLEPC_DIR}/$ENV{PETSC_ARCH}/lib/pkgconfig)
set(PATH $ENV{PATH})

# Remove the lines below if you do not wish to have PETSc determine the compilers
execute_process ( COMMAND pkg-config PETSc --variable=ccompiler COMMAND tr -d '\n' OUTPUT_VARIABLE C_COMPILER)
SET(CMAKE_C_COMPILER ${C_COMPILER})
execute_process ( COMMAND pkg-config PETSc --variable=cxxcompiler COMMAND tr -d '\n' OUTPUT_VARIABLE CXX_COMPILER)
if (CXX_COMPILER)
  SET(CMAKE_CXX_COMPILER ${CXX_COMPILER})
endif (CXX_COMPILER)
execute_process ( COMMAND pkg-config PETSc --variable=fcompiler COMMAND tr -d '\n' OUTPUT_VARIABLE FORTRAN_COMPILER)
if (FORTRAN_COMPILER)
  SET(CMAKE_Fortran_COMPILER ${FORTRAN_COMPILER})
  enable_language(Fortran)
endif (FORTRAN_COMPILER)

if ("${CMAKE_Fortran_COMPILER_ID}" STREQUAL "GNU")
  set (std -std=f2008)
  set (prepp -cpp)
  set (arg "-fopenmp -fallow-argument-mismatch -ffree-line-length-none -fimplicit-none -finit-real=nan -fmax-errors=20")
  set (check "-fbacktrace -fbounds-check")
  set(CMAKE_Fortran_FLAGS "${std} ${prepp} ${arg} ${check}")
  add_link_options(-fopenmp)
elseif ("${CMAKE_Fortran_COMPILER_ID}" STREQUAL "Intel")
  # add_compile_options(-cpp -qopenmp -traceback -CB -check all -stand f08)
  set (std -std08)
  set (prepp -cpp)
  set (arg "-qopenmp -w -qmkl")
  # set (check "-traceback -CB -check all")
  set (check "-traceback -CB")
  set(CMAKE_Fortran_FLAGS "${std} ${prepp} ${arg} ${check}")
  add_link_options(-qopenmp -qmkl -w)
elseif ("${CMAKE_Fortran_COMPILER_ID}" STREQUAL "IntelLLVM")
set (std -std08)
set (prepp -cpp)
set (arg "-qopenmp -w -qmkl")
  # set (check "-traceback -CB -check all")
  set (check "-traceback -CB")
set(CMAKE_Fortran_FLAGS "${std} ${prepp} ${arg} ${check}")
add_link_options(-qopenmp -qmkl -w)
endif()

# --- find preprocessor
find_program(FYPP fypp)
if(NOT FYPP)
  message(FATAL_ERROR "Preprocessor fypp not found! Please install fypp following the instructions in https://fypp.readthedocs.io/en/stable/fypp.html#installing")
endif()


#  tells CMake to build the application ex1 from the source file ex1.c
#  this must appear AFTER the compilers are set
project(Chem)
set(CMAKE_INSTALL_PREFIX $ENV{HOME})

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/cmake")
include(utils)
include(FetchContent)

FetchContent_Declare(
fortran-yaml-c
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../fortran-yaml-c
)
FetchContent_MakeAvailable(fortran-yaml-c)

find_package(PkgConfig REQUIRED)
pkg_search_module(fortran_stdlib REQUIRED IMPORTED_TARGET fortran_stdlib)
# pkg_search_module(PETSC REQUIRED IMPORTED_TARGET PETSc)
# pkg_search_module(toml-f REQUIRED IMPORTED_TARGET toml-f)
# pkg_search_module(cantera REQUIRED IMPORTED_TARGET cantera)

add_subdirectory(src)
enable_testing()
add_subdirectory(test)
